Logging
| Document status | 35 - Reviewed |
|---|
Logging is a fundamental part of the software development lifecycle. It plays a critical role in capturing errors, diagnosing issues, and understanding system behavior. Proper logging goes hand-in-hand with robust error handling and supports developers during troubleshooting when failures occur.
Log Content
Determining what information to log and when is a key aspect of designing an effective logging strategy. Logging too much data can overwhelm the system and make it difficult to locate meaningful information, while logging too little can leave developers blind to the causes of production issues. Striking the right balance requires judgment and context awareness from every developer.
During early development, it is often better to log more information than you think you need. Once the application reaches production, perform regular log reviews to tune the verbosity and focus on what is truly useful.
In general, the following types of events are strong candidates for logging:
- Application errors and exceptions
- Authentication, authorization, and session-related activities
- Input validation failures or suspicious inputs
- Interactions with external or third-party services
- Performance data for resource-intensive operations
Capturing these categories consistently provides the essential context for understanding system behavior and diagnosing issues efficiently.
Log Levels
Besides deciding what to log, it’s equally important to choose the appropriate log level to indicate the severity and relevance of each event. Microsoft.Extensions.Logging support a common set of levels, which can be used to control the volume of logged data and filter events by importance.
The available log levels are:
- Critical – Represents a system-wide failure from which the application cannot recover (e.g. database unavailable).
- Critical events should trigger immediate alerts or operational notifications.
- Error – Used when an issue prevents one or more features from functioning correctly.
- Error events should result in an action point and requires attention from developers
- Warning – Indicates unexpected or abnormal behavior that does not stop execution but might cause issues later.
- Useful for identifying early signs of failure.
- Information – Describes routine application actions or state changes (e.g., user logins, scheduled jobs).
- These events are purely informative and can be ignored during normal operation.
- Debug – Intended for troubleshooting during development or in lower environments.
- Provides detailed information useful for diagnosing problems.
- Trace – The most granular level, offering a step-by-step record of application activity.
- Typically used only when deep visibility into the code or third-party libraries is required and should be avoided in production due to verbosity.
Log level hierarchy:
Trace > Debug > Information > Warning > Error > Critical
Logging levels can be configured to control output. For example, if the log minimum level is set to Warning, only Warning, Error, and Critical messages will be recorded.